package org.boyan.modules.cgreport.service.a;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.boyan.common.exception.JeecgBootException;
import org.boyan.common.system.api.ISysBaseAPI;
import org.boyan.common.util.oConvertUtils;
import org.boyan.modules.cgreport.entity.OnlCgreportHead;
import org.boyan.modules.cgreport.service.IOnlCgreportAPIService;
import org.boyan.modules.cgreport.service.IOnlCgreportItemService;
import org.boyan.modules.cgreport.service.IOnlCgreportParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author: zhuxiaobao
 * @time: 2021/9/26 9:46
 */

@Service("onlCgreportAPIService")
public class b implements IOnlCgreportAPIService {
    private static final Logger a = LoggerFactory.getLogger(b.class);

    @Autowired
    private c onlCgreportHeadService;

    @Autowired
    private IOnlCgreportItemService onlCgreportItemService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private IOnlCgreportParamService onlCgreportParamService;

    @Override
    public Map<String, Object> getDataById(String id, Map<String, Object> params) {
        return getData(id, null, params);
    }

    @Override
    public Map<String, Object> getDataByCode(String code, Map<String, Object> params) {
        return getData(null, code, params);
    }

    @Override
    public Map<String, Object> getData(String id, String code, Map<String, Object> params) {
        OnlCgreportHead onlCgreportHead = null;
        if (oConvertUtils.isNotEmpty(id)) {
            onlCgreportHead = this.onlCgreportHeadService.getById(id);
        } else if (oConvertUtils.isNotEmpty(code)) {
            LambdaQueryWrapper<OnlCgreportHead> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OnlCgreportHead::getCode, code);
            onlCgreportHead = this.onlCgreportHeadService.getOne(lambdaQueryWrapper);
        }
        if (onlCgreportHead == null) {
            throw new JeecgBootException("实体不存在");
        }
        try {
            String str1 = onlCgreportHead.getCgrSql().trim();
            String str2 = onlCgreportHead.getDbSource();
            return executeSelectSqlRoute(str2, str1, params, onlCgreportHead.getId());
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            throw new JeecgBootException("SQL执行失败：" + exception.getMessage());
        }
    }

    @Override
    public Map<String, Object> executeSelectSqlRoute(String dbKey, String sql, Map<String, Object> params, String headId) throws Exception {
        if (StringUtils.isNotBlank(dbKey)) {
            a.debug("Online报表: 走了多数据源逻辑");
            return this.onlCgreportHeadService.executeSelectSqlDynamic(dbKey, sql, params, headId);
        }
        a.debug("Online报表: 走了稳定逻辑");
        return this.onlCgreportHeadService.executeSelectSql(sql, headId, params);
    }
}
